import requests
import csv
import date_time
import file

class weather():

    def __init__(self):
        self.apk = '35db7aae1c46785a452cdf2055c69cb2'
        self.time = date_time.today()
        self.path = 'E:\python\winner\college\interface\AM.csv'

    def get_city(self):
        data = csv.reader(open(self.path, 'r', encoding='UTF-8'))
        return data

    def get_json(self, city_code):
        url = "https://restapi.amap.com/v3/weather/weatherInfo?city={}&key={}".format(city_code, self.apk)
        p = requests.get(url=url, timeout=30)
        json = p.json()
        return json

    def main(self):
        rst = []
        key = 1
        data = self.get_city()
        for line in data:
            json = self.get_json(line[1])
            x = [self.time, str(json)]
            _str = '\001'.join(x)
            rst.append(_str)
            key += 1
            if key > 10:
                break
        return rst


if __name__ == '__main__':
    itf = weather()
    data_weather = itf.main()
    file.wt_array('E:\python\winner\college\interface\weather.txt',data_weather)